package com.xmkj.winmoney.mine.activity;

import android.view.View;
import android.widget.TextView;

import com.common.bean.MoneyCarBean;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.entity.bean.UserDetailsBean;
import com.common.retrofit.methods.GetMoneyMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.widget.navigation.WidgetButton;
import com.xmkj.winmoney.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action1;

/**
 * Created by chen on 2018/3/31.
 */

public class MyWalletActivity extends BaseMvpActivity {
    @BindView(R.id.tv_count)
    TextView mTvCount;
    @BindView(R.id.tv_chongzhi)
    TextView mTvChongzhi;
    @BindView(R.id.tv_tixian)
    TextView mTvTixian;
    private String currenMoney;
    private UserDetailsBean userDetailsBean;


    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mywallet;
    }

    @Override
    protected void onViewCreated() {
        userDetailsBean = (UserDetailsBean) getIntent().getSerializableExtra(Constans.INFOBEAN);
        setRightNavigationText();
    }
    private void setRightNavigationText() {
        m_navigationBar.setVisibility(View.VISIBLE);
        WidgetButton widgetButton = new WidgetButton(this, R.string.user_car_details);
        widgetButton.getTextView().setTextColor(getResources().getColor(R.color.black));
        widgetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                gotoUpdateUser();
                gotoActivity(UserCarDetailsActivity.class);
            }
        });
        m_navigationBar.setRightMenu(widgetButton);
    }

    @Override
    protected void doLogicFunc() {

        attachClickListener(mTvChongzhi);
        attachClickListener(mTvTixian);
        gotoGetMoney();
        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.MONEY_CHANGE)&&(Boolean) rxKeyEvent.getValue()) {
                    gotoGetMoney();

                }
            }
        }));
    }

    private void gotoGetMoney() {
        CommonSubscriber<MoneyCarBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                MoneyCarBean infoBean = (MoneyCarBean) o;
                dismissProgressDialog();
                mTvCount.setText(infoBean.account);
                currenMoney = infoBean.account;
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        GetMoneyMethods.getInstance().orderinfo(subscriber, SPUtils.getShareInt(Constans.UID), SPUtils.getShareString(Constans.HASHID),userDetailsBean.detail_id);
        rxManager.add(subscriber);
    }

    @Override
    protected void onViewClicked(View view) {
        if(view.getId()==mTvChongzhi.getId()){
            gotoActivity(ChongZhiActivity.class);
        } else if(view.getId()==mTvTixian.getId()){
          gotoActivity(TiXianActivity.class);
        }
    }



    @Override
    protected void setNavigation() {
        setNavigationBack("我的钱包");
    }

 
    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
